var app = getApp()  
const Constants = require('../../utils/constants');
const Util = require('../../utils/util');
const Api = require('../../utils/api.js');
const Rest = require('../../utils/rest');
const Auth = require('../../utils/auth');
const util = require('../../utils/util.js');
const { IsHaveStr,IsNotEmpty } = require('../../utils/util.js');
const WxParse = require('../../components/wxParse/wxParse');
const Poster = require('../../components/poster/poster/poster');
let rewardedVideoAd = null;

Page({

    data: {
        post: {},
        post_like: 0,
        post_favorite: 0,
        comment_count: 0,
        comments: [],
        loadding: false,
        pullUpOn: true,
        loaded: false,
        show_comment_submit: false,
        comment_content: '',
        placeholder:"喜欢就给个评论支持一下啦~",
        gzh: [],
        setting: {},
        comment_count_change: 0
        
    },
    default: {
        background: '../../images/default_avatar.png',
        description: '文章内容图片声明，请在插件后台＞蓝精灵插件＞文章设置中配置！',
    },
    post_id: 0,
    comment_id: 0,

    //小程序码
    wxacode: '',

    //返回页面是否需要刷新
    needRefresh: true,

    onLoad: function (options) {
        //获取公众号关注
        let that = this;
        Rest.get(Api.JIANGQIE_SETTING_HOME).then(res => {
            that.setData({
                gzh: res.data.gzh,
            });
        })
        Rest.get(Api.JIANGQIE_SETTING_wz).then(res => {
            util.getshare(that);
            that.setData({
                setting: {
                    background: res.data.background ? res.data.background : that.default.background,
                    title: res.data.title ? res.data.title : that.default.title,
                    title1: res.data.title1 ? res.data.title1 : that.default.title1,
                    description: res.data.description ? res.data.description : that.default.description,
                }
            });
        })
        if (options.scene) {
            this.post_id = decodeURIComponent(options.scene);
        } else if (options.post_id) {
            this.post_id = options.post_id;
        }

        //小程序码
        this.loadWxacode();
    }, 
    onShow: function () {
        var timestamp = Date.parse(new Date());
        var date = new Date(timestamp);
        var day = date.getDay();
        var weeks = new Array("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
        var week = weeks[day];
        var Y = date.getFullYear();
        var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
        var D = date.getDate();
        var flashSaleDate = Y + '.' + M + '.' + D + ' ' + week;
        this.setData({
            flashSaleDate: flashSaleDate
        })
        if (!this.needRefresh) {
            this.needRefresh = true;
            return;
        }
       let that = this;
        Rest.get(Api.JIANGQIE_POST_DETAIL, {
            post_id: that.post_id
        }).then(res => {
            wx.setNavigationBarTitle({
                title: res.data.title,
            })

            that.setData({
                post: res.data,
                post_like: res.data.user.islike,
                post_favorite: res.data.user.isfavorite,
                comment_count: Number(res.data.comment_count),
                like_list: res.data.like_list,
            });

            WxParse.wxParse('article', 'html', res.data.content, that, 5);
        });

        this.loadComments(true);
    },

    onReachBottom: function () {
        if (!this.data.pullUpOn) {
            return;
        }

        this.loadComments(false);
    },

    onShareAppMessage: function () {
        return {
            title: this.data.post.title,
            imageUrl: this.data.post.thumbnail,
            path: 'pages/article/article?post_id=' + this.post_id,
        }
    },
    onShareTimeline: function () {
        return {
            title: this.data.post.title,
            query: 'post_id=' + this.post_id,
            imageUrl: this.data.post.thumbnail,
        }
    },
    fk: function()
  { 
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    })
},
// 获取滚动条当前位置
onPageScroll: function (e) {
    console.log(e)
    if (e.scrollTop > 100) {
      this.setData({
        floorstatus: true
      });
    } else {
      this.setData({
        floorstatus: false
      });
    }
  },
  goTop: function (e) {  
    if (wx.pageScrollTo) {
      wx.pageScrollTo({
        scrollTop: 0
      })
    } else {
      wx.showModal({
      })
    }
  },

    /**
     * 海报分享
     */
    sharePosterClick: function (e) {
        let posterConfig = {
            width: 750,
            height: 1334,
            backgroundColor: '#1E90FF',
            debug: false,
            pixelRatio: 1,
            blocks: [{
                width: 690,
                height: 1000,
                borderRadius: 50,
                x: 30,
                y: 234,
                backgroundColor: '#FFFFFF'
            }, ],
            texts: [{
                    x: 375,
                    y: 120,
                    baseLine: 'middle',
                    textAlign: 'center',
                    text: this.data.post.title,
                    width: 600,
                    fontSize: 38,
                    color: '#FFFFFF',
                },
                {
                    x: 70,
                    y: 780,
                    fontSize: 28,
                    lineHeight: 40,
                    baseLine: 'middle',
                    text: this.data.post.excerpt,
                    width: 600,
                    lineNum: 3,
                    color: '#000000',
                    zIndex: 200,
                },
                {
                    x: 360,
                    y: 1170,
                    baseLine: 'middle',
                    textAlign: 'center',
                    text:  this.data.shares.share_title,
                    fontSize: 28,
                    color: '#888888',
                    zIndex: 200,
                }
            ],
            images: [
                {
                    width: 690,
                    height: 520,
                    borderRadius: 50,
                    x: 30,
                    y: 200,
                    url: this.data.post.thumbnail,
                    zIndex: 100
                },
                {
                    width: 200,
                    height: 200,
                    borderRadius: 160,
                    x: 275,
                    y: 920,
                    url: this.wxacode,
                }
            ]

        }

        this.setData({
            posterConfig: posterConfig
        }, () => {
            Poster.create(true); // 入参：true为抹掉重新生成 
        });
    },

    /**
     * 画报生成成功
     */
    onPosterSuccess(e) {
        this.needRefresh = false;

        const {
            detail
        } = e;
        wx.previewImage({
            current: detail,
            urls: [detail]
        })
    },

    /**
     * 画报生成失败
     */
    onPosterFail(err) {
        console.error(err);
    },
    wxParseTagATap: function (e) {
        wx.setClipboardData({
            data: e.currentTarget.dataset.src,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        wx.showToast({
                            title: '链接复制成功',
                            image: '../../images/link.png',
                            duration: 2000//持续的时间
                        });
                    }
                })
            }
        })
    },
    /**
     * 点击 TAG
     */
    handlerTagClick: function (e) {
        let tag_id = e.currentTarget.dataset.id;
        let tag = e.currentTarget.dataset.tag;
        wx.navigateTo({
            url: '/pages/list/list?title=' + tag + '&tag_id=' + tag_id,
        })
    },

    /**
     * 跳转返回
     */
    jumpBtn: function (options) {
        Util.navigateBack();
    },

    /**
     * 文章 点赞
     */
    handlerLikeClick: function (e) {
        let that = this;
        Rest.get(Api.JIANGQIE_USER_LIKE, {
            post_id: that.data.post.id
        }).then(res => {
            let avatar = Auth.getUser().avatar;
            var index = that.data.like_list.indexOf(avatar);
            if (index > -1) {
                that.data.like_list.splice(index, 1);
            } else {
                that.data.like_list.unshift(avatar);
            }

            that.setData({
                post_like: (that.data.post_like == 1 ? 0 : 1),
                like_list: that.data.like_list
            });
        })
    },

    /**
     * 评论 弹框
     */
    handlerCommentClick: function (e) {
        this.comment_id = 0;
        this.setData({
            show_comment_submit: true
        });
    },

    /**
     * 评论 取消
     */
    handlerCancelClick: function (e) {
        this.setData({
            show_comment_submit: false
        });
    },

    /**
     * 评论 提交
     */
    handlerCommentSubmit: function (e) {
        let that = this;
        Rest.get(Api.JIANGQIE_COMMENT_ADD, {
            post_id: that.post_id,
            parent_id: that.comment_id,
            content: that.data.comment_content
        }).then(res => {
            that.setData({
                comment_count_change: that.data.comment_count_change + (res.data.comment_verify == 1 ? 0 : 1),
                show_comment_submit: false
            });

            that.loadComments(true);
        });
    },

    /**
     * 评论 回复
     */
    handlerCommentReplyClick: function (e) {
        this.comment_id = e.currentTarget.dataset.id;
        this.setData({
            show_comment_submit: true
        });
    },

    /**
     * 评论 删除
     */
    handlerCommentDeleteClick: function (e) {
        let that = this;

        wx.showModal({
            title: '提示',
            content: '确定要删除评论吗？',
            success(res) {
                if (res.confirm) {
                    let comment_id = e.currentTarget.dataset.id;
                    Rest.get(Api.JIANGQIE_COMMENT_DELETE, {
                        comment_id: comment_id
                    }).then(res => {
                        that.setData({
                            comment_count_change: that.data.comment_count_change - 1
                        });
                        that.loadComments(true);
                    });
                }
            }
        });
    },

    /**
     * 评论输入
     */
    handlerContentInput: function (e) {
        this.setData({
            comment_content: e.detail.value
        });
    },

    /**
     * 文章 收藏
     */
    handlerFavoriteClick: function (e) {
        let that = this;
        Rest.get(Api.JIANGQIE_USER_FAVORITE, {
            post_id: that.data.post.id
        }).then(res => {
            that.setData({
                post_favorite: (that.data.post_favorite == 1 ? 0 : 1)
            });
        })
    },

    /**
     * 加载小程序码
     */
    loadWxacode: function () {
        let that = this;
        Rest.get(Api.JIANGQIE_POST_WXACODE, {
            post_id: that.post_id
        }).then(res => {
            that.wxacode = res.data;
        }, err => {
            console.log(err);
        });
    },

    /**
     * 加载 评论
     */
    loadComments: function (refresh) {
        let that = this;

        that.setData({
            loadding: true
        });

        let offset = 0;
        if (!refresh) {
            offset = that.data.comments.length;
        }

        Rest.get(Api.JIANGQIE_COMMENT_INDEX, {
            post_id: that.post_id,
            offset: offset
        }).then(res => {
            that.setData({
                loaded: true,
                loadding: false,
                comments: refresh ? res.data : that.data.comments.concat(res.data),
                pullUpOn: res.data.length >= Constants.JQ_PER_PAGE_COUNT,
            });
        });
    },
//tab -- end

handlerIconNavClick: function(e) {
    let link = e.currentTarget.dataset.link;
    this.openLink(link);
},
handlericonNacClick: function(e) {
    let link = e.currentTarget.dataset.link;
    this.openLink(link);
},

handlerActiveClick: function(e) {
    let link = e.currentTarget.dataset.link;
    this.openLink(link);
},

handlerArticleClick: function (e) {
    let post_id = e.currentTarget.dataset.id;
    wx.navigateTo({
        url: '/pages/article/article?post_id=' + post_id
    })
},
/*h5跳转*/ 
viewUrl(res){  //内容中，跳转的外链接
let link =res.currentTarget.dataset.link;
console.info(link)
if (res &&IsNotEmpty(link)) {
    let isAddr =IsHaveStr(link,"http");
    /*console.info(link)*/
        wx.navigateTo({
            url:isAddr? '/pages/webview/webview?href=' + link:link
          }) 
     }
else if(!IsNotEmpty(link)){
    wx.showToast({
        title: '取消或未配置',
        image: '../../images/lj.png',
      })
   }
},
/*结束*/
 
//点击事件  
clickImg: function(e){
    var background = this.data.gzh[0].icon
    wx.previewImage({
      urls: [background], //需要预览的图片http链接列表，注意是数组
      current: '', // 当前显示图片的http链接，默认是第一个
      success: function (res) { },
      fail: function (res) { },
      complete: function (res) { },
    })
},
/*返回首页*/ 
goHome: function () {
    wx.switchTab({
      url: '../index/index'
    })
  },
    handlerLoginCancelClick: function (e) {
        this.setData({
            showPopLogin: false
        });
    },
    dasweixin: function (e) {
        wx.navigateTo({
            url: '/pages/dashang/dashang',
        });
    },
    handlerDoLoginClick: function (e) {
        wx.navigateTo({
            url: '/pages/login/login',
        });

        this.setData({
            showPopLogin: false
        });
    },
})


